def balance():
    import tkinter as tk  # 导入窗口
    from tkinter import ttk
    from datetime import datetime
    now = datetime.now()  # 读取当前时间
    with open("balance.txt", "r", encoding="utf-8") as f:
        content =f.read().strip()

        f.read().strip()
        money = float(content)
        window = tk.Tk()
        window.title("支付模拟器")
        window.geometry("300x200")
        label1 = tk.Label(window, text="balance", font=0)  # “余额”
        label1.pack()
        balance = tk.Label(window, text="¥" + str(money))  # 余额数量
        balance.pack()

        def onbutton():
            window2 = tk.Tk()  # 充值界面
            window2.title("Recharge")
            window2.geometry("200x200")
            label3 = tk.Label(window2, text="Recharge amount")  # “充值金额”
            label3.pack()
            entry2 = tk.Entry(window2)  # 输入充值的金额
            entry2.pack()
            label5 = ttk.Label(window2, text="¥")  # “符号”
            label5.place(x=12, y=18)

            def onbutton2():
                try:
                    with open("balance.txt", "r", encoding="utf-8") as f:
                         content = f.read().strip()
                         money = float(content)
                    money = money + float(entry2.get())  # 获取输入的充值金额
                    balance.config(text="¥" + str(money))  # 充值后对余额进行修改
                    with open("history.txt","a",encoding="utf-8") as f:
                        content = f"Time:{now}, Recharge {entry2.get()} ¥.\n"
                        f.write(content)
                    window2.destroy()  # 关闭充值界面
                    window3 = tk.Tk()  # 充值成功界面
                    window3.geometry("100x100")
                    label4 = tk.Label(window3, text="Recharge successful")  # “充值成功”
                    label4.pack()
                    with open("balance.txt", 'w') as f:
                        f.write(str(money))
                except:
                    window4=tk.Tk()
                    window4.geometry("100x100")
                    label5=tk.Label(window4,text="Error")
                    label5.pack()



            button2 = tk.Button(window2, text="recharge", command=onbutton2)  # 点击充值的按钮
            button2.pack()

        button = ttk.Button(window, text="recharge", command=onbutton)  # 进入充值界面按钮
        button.place(x=105, y=50)

        def onbutton3():
            with open("balance.txt", "r", encoding="utf-8") as f:
                content = f.read().strip()
                money = float(content)
            balance.config(text="€" + str(money * 0.129))  # 人民币转换欧元
            balance.pack()
            return money

        button3 = ttk.Button(window, text="Display Euro", command=onbutton3)  # 显示欧元按钮
        button3.place(x=60, y=80)

        def onbutton4():
            with open("balance.txt", "r", encoding="utf-8") as f:
                content = f.read().strip()
                money = float(content)
            balance.config(text="¥" + str(money))  # 欧元转换人民币
            balance.pack()

        button4 = ttk.Button(window, text="Display RMB", command=onbutton4)  # 显示人民币按钮
        button4.place(x=150, y=80)

